<?php


namespace App\store\controllers\goods;


use App\store\controllers\StoreBaseController;
use App\store\models\GoodsModel;
use App\store\services\GoodsServices;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class GoodsController extends StoreBaseController
{
    protected $class_name = 'Goods';

    /**
     * 商品列表
     */
    public function getGoodsList(Request $request)
    {
        $data = self::getStoreData($this->class_name,'getGoodsList', 2);
        if ($data) {
            return $data;
        }
        $data = $request->all();
        $validator = Validator::make(
            $data,
            GoodsModel::rules(),
            GoodsModel::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return errorJson($msg[0], []);
        }
        $list = GoodsServices::getGoodsList($data);
        return successJson('获取成功!', $list);
    }

    /**
     * 添加/编辑商品
     */
    public function modifyGoods(Request $request)
    {
        $data = self::getStoreData('Goods','modifyGoods', 2);
        if ($data) {
            return $data;
        }
        $data = $request->all();
        $validator = Validator::make(
            $data,
            GoodsModel::modify_rules(),
            GoodsModel::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return errorJson($msg[0], []);
        }
        $list = GoodsServices::modifyGoods($data);
        if (!$list) {
            return errorJson('操作失败！', []);
        }
        return successJson('操作成功！', []);
    }

    /**
     * 下架/删除商品
     */
    public function removalDelGoods(Request $request)
    {
        $data = self::getStoreData('Goods','removalDelGoods', 2);
        if ($data) {
            return $data;
        }
        $data = $request->all();
        $validator = Validator::make(
            $data,
            GoodsModel::update_rules(),
            GoodsModel::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return errorJson($msg[0], []);
        }
        $list = GoodsServices::updateGoodStatus($data);
        return successJson('操作成功！', $list);
    }

}
